#
# @lc app=leetcode.cn id=977 lang=python3
#
# [977] 有序数组的平方
#
from typing import List
# @lc code=start
class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
        ans = [0] * len(nums)
        left = 0
        right = len(nums) - 1
        idx = len(nums) - 1
        while left <= right:
            if abs(nums[left]) < abs(nums[right]):
                ans[idx] = nums[right] ** 2
                right -= 1
            else:
                ans[idx] = nums[left] ** 2
                left += 1
            idx -= 1
        return ans

# @lc code=end

